﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EC.Application.Impl
{
    using Dapper;
    using EC.Application.Interface;

    using EC.Entity;
    using EC.Entity.Request.DepositOrder;
    using EC.Entity.Response.DepositOrder;
    using EC.Entity.Tables;
    using System.Linq;

    /// <summary>
    /// 标签接口实现
    /// </summary>
    public class CashServiceImpl: CashService
    {
        #region 注入
        /// <summary>
        /// 数据接口
        /// </summary>
        private readonly IDBSession _dbSession;

        /// <summary>
        /// 数据库上下文
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public CashServiceImpl(IDBSession dbSession)
        {
            _dbSession = dbSession;
        }
        #endregion

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JResult Save(SaveCashRequest request)
        {
            var response = new JResult<CashResponse>()
            {
                Success = false
            };

            try
            {
                //商户
                var merchant = _dbSession.DbConnection.QueryFirst<Merchant>("SELECT * FROM merchant WHERE Id=@Id", new { Id = request.Id });
                if(merchant==null)
                {
                    throw new Exception("参数无效.");
                }
                //获取银行卡
                var bankCard = _dbSession.DbConnection.QueryFirst<BankCard>("SELECT * FROM bankcard WHERE MerchantId=@MerchantId AND CardType=10 AND Frozen=1 AND Status=1 ORDER BY BalanceUpdateTime DESC", new { MerchantId = merchant.Id });
                if (bankCard == null)
                {
                    throw new Exception("参数无效.");
                }
                //存款订单
                var depositOrderExist = _dbSession.DbConnection.Query<DepositOrderResponse>("SELECT * FROM depositorder WHERE MerchantId=@MerchantId and MerchantOrderNo=@MerchantOrderNo", new { MerchantId=merchant.Id, MerchantOrderNo = request.MerchantOrderNo });
                if (depositOrderExist != null && depositOrderExist.Any())
                {
                    throw new Exception("订单已存在.");
                }


                var model = new DepositOrder();

                //MerchantOrderNo
                //var lableNameExist = _dbSession.DbConnection.Query<DepositOrderResponse>("SELECT * FROM label WHERE Name=@Name", new { Name = request.Name });
                //if (lableNameExist != null && lableNameExist.Any())
                //{
                //    throw new Exception("标签名称已存在.");
                //}

                //var lable = new Label()
                //{
                //    Name = request.Name,
                //    Status = request.Status.Value,
                //    DisplayOrder = int.Parse(request.DisplayOrder),
                //    CreatedBy = request.CreatedBy,
                //    CreatedDate = DateTime.Now
                //};

                ////新增
                //long resultId = _dbSession.DbConnection.Insert<Label>(lable);

                //if (resultId <= 0)
                //{
                //    throw new Exception("新增标签失败.");
                //}
                response.Success = true;
            }
            catch(Exception ex)
            {

            }
            return response;
        }

    }
}
